package com.pascal91.webpp.controller;

import com.pascal91.webpp.model.Users;
import com.pascal91.webpp.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomePageController {

    @Autowired
    UsersService usersService;

    @RequestMapping(value = "/")
    public ModelAndView begin() {
        return new ModelAndView("redirect:/home.html");
    }

    @RequestMapping("/home.html")
    public ModelAndView home(Model model) {
        Users users = new Users();
        model.addAttribute("user", users);
        model.addAttribute("userList", usersService.getUsersList());

        return new ModelAndView("home");
    }

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public ModelAndView saveUser(Users user){
        usersService.addUser(user);
        return new ModelAndView("redirect:/home.html");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView saveUser(@RequestParam(value = "id", required = true) Long id){
        usersService.deleteUserByID(id);
        return new ModelAndView("redirect:/home.html");
    }

    @RequestMapping("/homeSS.htm")
    public ModelAndView homeSS(Model model) {
        return new ModelAndView("homeSS");
    }

}
